<?php
namespace Admin\Model;
use Think\Model;
class RoleModel extends Model {
    /**
     * 检测分类是否存在
     */
    public function name_exists($name, $id=0) {
        $where = "name='" . $name . "' AND id<>'" . $id . "'";
        $result = $this->where($where)->count('id');
        if ($result) {
            return true;
        } else {
            return false;
        }
    }

    /*
     *  检查用户权限
     */
    public function check_auth ($mid, $uid){
        //菜单模块和方法名字
        $menus = $this->table('yk_menu')->where (array ('id'=>$mid))->find ();
        //用户角色
        $role_id = $this->table('yk_admin')->where (array ('id'=>$uid))->getField('role_id');
        //如果是超管理直接返回真
        if ($role_id == C ('SUPERADMIN_ROLEID')) return true;
        //角色权限
        $role_auths = $this->table('yk_role_menu')->where (array ('roleid'=>$role_id))->getField('id,module_name,action_name');
        $module = array ();
        $act = array ();
        foreach ($role_auths as $ro){
            $module[] = $ro['module_name'];
            $act[] = $ro['action_name'];
        }
        if (in_array ($menus['module_name'], $module) && in_array($menus['action_name'], $act)){
            return true;
        } else {
            return false;
        }
    }

}